package TenoriOn.managers
{
	import TenoriOn.Grid;
	import TenoriOn.GridData;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	public class SequencerManager implements IGridManager
	{
		public function SequencerManager()
		{
			_sequence=new GridData();
		}
		
		public function get MANAGER_TYPE():String
		{
			return "SequencerManager";
		}
		
		public function makeGridData():GridData
		{
			return _sequence;
		}
		
		public function takeControl(grid:Grid):void
		{
			grid.manager=this;
			_grid=grid;
			_hasControl=true;
			updateGrid();
		}
		
		public function releaseControl():void
		{
			_hasControl=false;
		}
		
		public function onGridUpdate(id:Point):void
		{
			_sequence.data[id.x][id.y]=_sequence.data[id.x][id.y]?0:1;
			updateGrid();
		}
		
		public function updateGrid():void
		{
			_grid.textDisplay.setText("Tenori-On","SEQUENCER",_soundManager.playing?"PLAYING":"STOPPED");
			
			if(_hasControl){
				if(_soundManager.playing){
					var gData:GridData=new GridData();
					for (var i:*=0; i<16; i++){
						gData.data[i]=_sequence.data[i].concat();
					}
					for (var j:*=0; j<16; j++){
						gData.data[_soundManager.position][j]+=1;
					}
					_grid.redrawGrid(gData);
				}
				else
					_grid.redrawGrid(_sequence);
			}
		}
		
		public function clearGrid():void{
			_sequence=new GridData();
			updateGrid();
		}
		
		private var _hasControl:Boolean;
		public function get hasControl():Boolean{
			return _hasControl;
		}
		
		private var _grid:Grid;
		
		public var _sequence:GridData;
		
		private var _soundManager:SoundManager;
		public function set soundManager(value:SoundManager):void{_soundManager=value;}
	}
}